package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    SysUserMapper userMapper;

    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    @Autowired
    RedisTemplate<String, String> redisTemplate;
    /**
     * 注册: 该手机号 在库中不存在
     * 登录，重置密码，更换管理员: 该手机号在库中必须存在
     * @param mobile
     * @param type
     */
    @Override
    public void sendSmsCode(String mobile, String type) {
        //健壮性的判断
        if(mobile == null || type == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        LambdaQueryWrapper<SysUser> userQw = new LambdaQueryWrapper<>();
        userQw.eq(SysUser::getMobile, mobile);

        //查询该手机号对应的 用户对象
        SysUser user = userMapper.selectOne(userQw);
        //如果type是2 ，说明在注册，该手机号在 库中应该是不存在
        if(type.equals("2")){
            //如果查到了
            if(user != null){
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        }
        //否则说明 是 登录，重置密码，更换管理员: 该手机号在库中必须存在
        else{
            if(user == null){
                throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
            }
        }


        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        //发送验证码到 用户手机
//        Boolean flag = aliyunSmsUtils.sendSMS(mobile, code);
//        if(!flag){
//            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
//        }
        log.info("验证码：" + code);

        //定义 redis中的key
        String key = getPrefix(type) + mobile;
        //存储验证码到redis
        redisTemplate.boundValueOps(key).set(code, 5, TimeUnit.MINUTES);


    }

    @Override
    public Boolean verify(String mobile, String type, String checkcode) {
        //健壮性的判断
        if(mobile == null || type == null || checkcode == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        String key = getPrefix(type) + mobile;

        //获取redis中的验证码
        String redisCode = redisTemplate.boundValueOps(key).get();

        if(redisCode == null){
            throw new NcException(ResponseEnum.INVALID_VERIFY_CODE);
        }
        boolean flag = redisCode.equals(checkcode);

        if(!flag){
            throw new NcException(ResponseEnum.INVALID_VERIFY_CODE);
        }
        //验证成功后， 删除对应的key
        redisTemplate.delete(key);

        return flag;
    }

    /**
     * 获取不同的场景的不同前缀
     * @param type
     * @return
     */
    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
        }
        return null;
    }
}
